package org.me.client;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.gwt.components.client.Canvas;

public class Graphes {

	public static void listPlot2D(float[][] A, Canvas ctx, float[][] intv,
			float[] dx_dy) {
		int height = ctx.getHeight();
		int width = ctx.getWidth();
		float miinX = new Float((float) 10 / (float) 100 * width).floatValue();
		float miinY = new Float((float) 10 / (float) 100 * height).floatValue();
		float MaaxX = new Float((float) 90 / (float) 100 * width).floatValue();
		float MaaxY = new Float((float) 90 / (float) 100 * height)
				.floatValue();
		NummerMatrix Mat = new NummerMatrix(NumerischeOperationen.float2Float(A));
		float maaxAX;
		float maaxAY;
		float miinAX;
		float miinAY;
		if (intv == null) {
			maaxAX = NumerischeMethoden.derGroessteNummer(Mat.kolonne(1))
					.floatValue();
			maaxAY = NumerischeMethoden.derGroessteNummer(Mat.kolonne(2))
					.floatValue();
			miinAX = NumerischeMethoden.derKleinsteNummer(Mat.kolonne(1))
					.floatValue();
			miinAY = NumerischeMethoden.derKleinsteNummer(Mat.kolonne(2))
					.floatValue();
		} else {
			miinAX = intv[0][0];
			maaxAX = intv[0][1];
			miinAY = intv[0][0];
			maaxAY = intv[0][1];
		}
		if (Mat.equals(null) || Mat.eBreite() != 2) {
			ctx.beginPath();
			ctx.moveTo(miinX, height - miinY);
			ctx.setStrokeColor("red");
			ctx.lineTo(MaaxX, height - MaaxY);
			ctx.moveTo(miinX, height - MaaxY);
			ctx.lineTo(MaaxX, height - miinY);
			ctx.stroke();
			ctx.setFillStyle("red");
			ctx.arc((float) width / 2, (float) height / 2,
					(float) (width + height) / 100, (float) 0,
					2 * (float) Math.PI, false);
			ctx.fill();
		} else {
			ctx.beginPath();
			ctx.setStrokeColor("green");
			ctx.setLineWidth(2);
			ctx.moveTo(miinX, height - miinY);
			ctx.lineTo(MaaxX, height - miinY);
			ctx.moveTo(miinX, height - miinY);
			ctx.lineTo(miinX, height - MaaxY);
			ctx.moveTo(MaaxX, height - miinY);
			ctx.lineTo(MaaxX, height - MaaxY);
			ctx.moveTo(miinX, height - MaaxY);
			ctx.lineTo(MaaxX, height - MaaxY);
			ctx.stroke();
			ctx.setLineWidth(1);
			ctx.moveTo(miinX, height - miinY);
			int lignX = 5;
			int lignY = 10;
			if (lignX > 0) {
				for (int i = 0; i < lignX; i++) {
					ctx.moveTo(miinX + (float) i / (float) lignX
							* (MaaxX - miinX), height - miinY);
					ctx.lineTo(miinX + (float) i / (float) lignX
							* (MaaxX - miinX), height - MaaxY);
				}
			}
			if (lignY > 0) {
				for (int i = 0; i < lignY; i++) {
					ctx.moveTo(miinX, height - miinY - (float) i
							/ (float) lignY * (MaaxY - miinY));
					ctx.lineTo(MaaxX, height - miinY - (float) i
							/ (float) lignY * (MaaxY - miinY));
				}
			}
			ctx.stroke();
			ctx.beginPath();
			ctx.setStrokeColor("rgba(0, 0, 200, 0.5)");
			ctx.setFillStyle("rgba(0, 0, 200, 0.5)");
			ctx.setLineWidth(1);
			ctx.moveTo(miinX, height - miinY);
			ctx.arc(miinX
					+ ((maaxAX - miinAX) - (maaxAX - Mat.sElementGeben(0, 0)
							.floatValue())) / (maaxAX - miinAX)
					* (MaaxX - miinX), height
					- miinY
					- ((maaxAY - miinAY) - (maaxAY - Mat.sElementGeben(0, 1)
							.floatValue())) / (maaxAY - miinAY)
					* (MaaxY - miinY), height / (float) 100, (float) 0,
					(float) (2 * Math.PI), false);
			ctx.fill();
			for (int i = 0; i < Mat.eLange() - 1; i++) {
				ctx.beginPath();
				ctx.arc(miinX
						+ ((maaxAX - miinAX) - (maaxAX - Mat.sElementGeben(
								i + 1, 0).floatValue())) / (maaxAX - miinAX)
						* (MaaxX - miinX), height
						- miinY
						- ((maaxAY - miinAY) - (maaxAY - Mat.sElementGeben(
								i + 1, 1).floatValue())) / (maaxAY - miinAY)
						* (MaaxY - miinY), height / (float) 100, (float) 0,
						(float) (2 * Math.PI), false);
				ctx.fill();
				ctx.moveTo(miinX
						+ ((maaxAX - miinAX) - (maaxAX - Mat
								.sElementGeben(i, 0).floatValue()))
						/ (maaxAX - miinAX) * (MaaxX - miinX), height
						- miinY
						- ((maaxAY - miinAY) - (maaxAY - Mat
								.sElementGeben(i, 1).floatValue()))
						/ (maaxAY - miinAY) * (MaaxY - miinY));
				ctx.lineTo(miinX
						+ ((maaxAX - miinAX) - (maaxAX - Mat.sElementGeben(
								i + 1, 0).floatValue())) / (maaxAX - miinAX)
						* (MaaxX - miinX), height
						- miinY
						- ((maaxAY - miinAY) - (maaxAY - Mat.sElementGeben(
								i + 1, 1).floatValue())) / (maaxAY - miinAY)
						* (MaaxY - miinY));
				ctx.stroke();
			}
		}
	}

	private static void printNum(Character ch, Canvas ctx, float[] coords) {

	}

	public Canvas plot2D(String funkt, Variable var, float[] intv) {
		// TODO listPlot2D,f(var)
		return null;
	}

	public static AbsolutePanel listPlot2DFlaeche(Canvas ctx) {
		AbsolutePanel ap = new AbsolutePanel();
		int l = ctx.getWidth();
		int h = ctx.getHeight();
		int L = (int) Math.round(ctx.getWidth() * ((float) 1 + 0.3));
		int H = (int) Math.round(ctx.getHeight() * ((float) 1 + 0.3));
		ap.setHeight(H + "px");
		ap.setWidth(L + "px");
		ap.add(ctx, L / 2 - l / 2, H / 2 - h / 2);
		// TODO: nums
		return ap;
	}

	public static Canvas Beispiel_EinesFroehlichenGeschichts() {
		final Canvas ctx = new Canvas(200, 200);
		ctx.beginPath();
		ctx.arc(75, 75, 50, 0, (float) Math.PI * 2, true); // Outer circle
		ctx.moveTo(110, 75);
		ctx.arc(75, 75, 35, 0, (float) Math.PI, false); // Mouth (clockwise)
		ctx.moveTo(65, 65);
		ctx.arc(60, 65, 5, 0, (float) Math.PI * 2, true); // Left eye
		ctx.moveTo(95, 65);
		ctx.arc(90, 65, 5, 0, (float) Math.PI * 2, true); // Right eye
		ctx.stroke();
		// path.curveToA(20,20,(float)2*Math.PI,true,true,20,30);
		return ctx;
	}

}
